package pl.kosmowska.ewelina.client.views.impl;

import gwtupload.client.IFileInput;
import gwtupload.client.IUploader;
import gwtupload.client.IUploader.OnStartUploaderHandler;
import gwtupload.client.SingleUploader;
import pl.kosmowska.ewelina.client.views.BankStatementImportView;
import pl.kosmowska.ewelina.client.views.images.UiImages;
import pl.kosmowska.ewelina.client.views.locale.UploaderLocaleConstants;
import pl.kosmowska.ewelina.client.views.widgets.MessageDialogBox;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;

public class BankStatementImportViewImpl extends Composite implements BankStatementImportView{

	private static StockViewImplUiBinder uiBinder = GWT
			.create(StockViewImplUiBinder.class);

	interface StockViewImplUiBinder extends UiBinder<Widget, BankStatementImportViewImpl> {
	}

	private Presenter presenter;
	private boolean alreadyInit;

	private OnStartUploaderHandler startUploadHandler;
	private IUploader.OnFinishUploaderHandler finishUploadHandler;
	private SingleUploader uploader = new SingleUploader(IFileInput.FileInputType.BUTTON);
	private UiImages uiImages = GWT.create(UiImages.class);

	@UiField
	HorizontalPanel mainPanel;
	Image imgLoading = new Image();



	public BankStatementImportViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
	}
	

	@Override
	public void reInitView(){
		initUploader();
		initLoadingWidget();
	}

	private void initUploader() {
		uploader.setAvoidRepeatFiles(true);
		UploaderLocaleConstants constants = new UploaderLocaleConstants();
		uploader.setI18Constants(constants);
		uploader.addOnStartUploadHandler(startUploadHandler);
		uploader.addOnFinishUploadHandler(finishUploadHandler);
		mainPanel.add(uploader);

	}

	private void initLoadingWidget() {
		imgLoading.setResource(uiImages.loading());
		imgLoading.setSize("20px", "20px");

		// set default state
		showLoading(false);
		mainPanel.add(imgLoading);
	}
	

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public boolean isAlreadyInit() {
		return alreadyInit;
	}
	@Override
	public void setAlreadyInit(boolean alreadyInit) {
		this.alreadyInit = alreadyInit;
	}
	@Override
	public void setFinishUploadHandler(IUploader.OnFinishUploaderHandler finishUploadHandler) {
		this.finishUploadHandler = finishUploadHandler;
	}
	@Override
	public void setStartUploadHandler(OnStartUploaderHandler startUploadHandler) {
		this.startUploadHandler = startUploadHandler;
		
	}

	@Override
	public SingleUploader getUploader() {
		return uploader;
	}


	@Override
	public void showLoading(boolean ifShow) {
		imgLoading.setVisible(ifShow);

	}

	@Override
	public void showMessage(String message) {
		MessageDialogBox messageDialogBox = new MessageDialogBox(message);
		messageDialogBox.center();
		messageDialogBox.show();

	}





}


